﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Configuration;
using System.Net.Mail;

namespace ElCharro01.Helpers
{
    public class MailHelper
    {

        public static void SendMailMessage(string to, string bcc, string cc, string subject, string body ,string from)
        {

            System.Net.Mail.MailMessage mMailMessage = new System.Net.Mail.MailMessage();

            mMailMessage.From = new System.Net.Mail.MailAddress(from);
            mMailMessage.To.Add(new System.Net.Mail.MailAddress(to));

            if (!string.IsNullOrEmpty(bcc))
                mMailMessage.Bcc.Add(new System.Net.Mail.MailAddress(bcc));
            
            if (!string.IsNullOrEmpty(cc))
                mMailMessage.CC.Add(new System.Net.Mail.MailAddress(cc));
            
            mMailMessage.Subject = subject;
            mMailMessage.Body = body;
            mMailMessage.IsBodyHtml = true;
            mMailMessage.Priority = System.Net.Mail.MailPriority.Normal;

            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(ConfigurationManager.AppSettings["SMTP"]);
            smtp.UseDefaultCredentials = false;
            if(!(ConfigurationManager.AppSettings["SMTPUsername"] == ""))
                smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["SMTPUsername"], System.Configuration.ConfigurationManager.AppSettings["SMTPPassword"]);
            smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
            smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["Ssl"]); ;

            smtp.Send(mMailMessage);
        }
    }
}
